<?php

class BlogController extends GTL_Action {

    public function init() {
        parent::init();
            /* Initialize action controller here */
            $this->list_sess_unset('controller_blog');
            $this->session_search_key = 'controller_blog';
            $this->session_search_name = 'search_label';
    }

    public function indexAction() {
		$blog_id = '';
		$cat_id = '';
		$recentData = '';
        $searchtype = '';
        $blogData = array();
        $blogModel = new Model_Blog();
		
		//category data in blog
		$categoryModel = new Model_Category();
		//$categoryArray = $categoryModel->fetchCategory("cat_status = 1");
		$categoryalldata = $categoryModel->fetchCategory($cat_id);
		$this->view->categoryalldata = $categoryalldata;
		
		//date data in blog
		$archive = '';
		$blogModel = new Model_Blog();
		//$categoryArray = $categoryModel->fetchCategory("cat_status = 1");
		$monthdata = $blogModel->fetchblogmonth($archive);
		$this->view->monthdata = $monthdata;
		
        $orderField = 'blog_id';
        $sort = 'desc';
        $where = '';
        $group_by = 'blog_id';
		
        /* Pagination Login */
        $itemsPerPageReviews = $this->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE
        if ($this->_getParam('page') && $this->_getParam('page') != '') {
            $currentPageReviews = $this->_getParam('page');
        }
        
        if ($this->_getParam('cat_id') && $this->_getParam('cat_id') != '') {
            $category_id = $this->_getParam('cat_id');
            $where = 'blog_cat_id ='.$category_id ; 
        }
        if($this->_getParam('tag_id') && $this->_getParam('tag_id') != ''){
            $tag_id = $this->_getParam('tag_id');
            $where = ' bt_tag_id = '.$tag_id;
            $group_by = 'bt_blog_id';
        }
        if(array_key_exists('blog_title',$_POST)){
            $where = 'blog_title like "%'.$_POST['blog_title'].'%"';
        }
       
        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
	
		if ($this->_request->isPost()) {
		
            $postedData = $this->_request->getPost();
            
            /* search code here */
            if ($postedData['searchtype'] == '') {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
            }
            if (isset($postedData['searchtype']) && ($postedData['searchtype'] != '')) {
                $searchtype = $postedData['searchtype'];
                $where = " blog_title LIKE '%$searchtype%' ";
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                $this->view->searchtype = $searchtype;
            }
        } else {

            if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
            }
            $searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
            $where = " blog_title LIKE '%$searchtype%' ";
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
			$this->view->searchtype = $searchtype;
        }

        $blogData = $blogModel->fetchBlogWITHComment($where,$itemsPerPageReviews, $offset, $orderField, $sort,$group_by);
        //$recentData = $blogModel->fetchBlogWITHComment();
        $countrecentData = $blogModel->fetchBlogWITHComment($where);
        
        $paginator = new GTL_Paginatorfront();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal(count($countrecentData));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('module' => 'default', 'controller' => 'blog', 'action' => 'index', 'page' => 'PAGENO'), null, false);
        $paginator->paginate($link);
        
        $GTLOBJ = new GTL_Common();
       
        $this->view->paginator = $paginator;
        $this->view->gtlObj = $GTLOBJ;
        //$this->view->recentData = $recentData;
        $this->view->blogData = $blogData;
		$this->view->searchtype = $searchtype;
    }

	
	public function blogdetailAction() {
		
		$blog_id = '';
		$cmt_id = '';
		$searchtype = '';
		$value = '';
		$cmt_send = '';
		$errorMessage = Array();
		$errorMessageArray = Array();
        $data = array();
        $commentData = Array();
		$successmessage = '';
		$this->view->mode = 'Add';
		
		// Load blog model
		$blogModel = new Model_Blog();
		
		//Load blog Form
        $blogdetailForm = new Form_BlogdetailForm();
		
		//Load the email send array for the check box in form 
       // $checkArray = $blogModel->getCheckArray();
       // $blogdetailForm->cmt_send->addMultiOptions($getCheckArray);
		
		//Load blogdetail Form
		$blogdetailForm = new Form_BlogdetailForm();
		
		//Load contact Model
		$commentModel = new Model_Comment();
			
		$cat_id = '';
		$categoryModel = new Model_Category();
		//$categoryArray = $categoryModel->fetchCategory("cat_status = 1");
		$categoryalldata = $categoryModel->fetchCategory($cat_id);
		$this->view->categoryalldata = $categoryalldata;
		
		//date data in blog
		
		$archive = '';
		$blogModel = new Model_Blog();
		//$categoryArray = $categoryModel->fetchCategory("cat_status = 1");
		$monthdata = $blogModel->fetchblogmonth($archive);
		$this->view->monthdata = $monthdata;
		
		if ($this->_request->isPost()) {
					
            $postedData = $this->_request->getPost();
			
            /* search code here */
			if (array_key_exists('searchtype' , $_POST)){
				if ($postedData['searchtype'] == '') {
					$_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
				}
				if (isset($postedData['searchtype']) && ($postedData['searchtype'] != '')) {
					$searchtype = $postedData['searchtype'];
					$where = " blog_title LIKE '%$searchtype%' ";
					$_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
					$this->view->searchtype = $searchtype;
				}
			} else {

				if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
					$_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
				}
				$searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
				$where = " blog_title LIKE '%$searchtype%' ";
				$_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
				$this->view->searchtype = $searchtype;
			}
			}
		
		if ($this->_getParam('blog_id') && $this->_getParam('blog_id') != '') {
                $blog_id = $this->_getParam('blog_id');
                $blogData = $blogModel->fetchBlogWITHComment("blog_id = ".$blog_id);
                if($blogData[0]['blog_status'] != 1){
                    $this->_helper->redirector('index', 'blog', 'default');
                }else{
                    $blog_id = $this->_getParam('blog_id');
                }
        } else {
                $this->_helper->redirector('index', 'blog', 'default');
        }
		
		if ($this->getRequest()->isPost()) {
			
			//Check for Errors
                $postedData = $this->_request->getPost();
                if (!$blogdetailForm->isValid($postedData)) {
					
                    $errorMessage = $blogdetailForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                   
                    if (count($errorMessageArray) > 0) {

                    } 
					else {
						
						/* send mail */	
						$body = "<html>
							<head>
							</head>
							<body>
								<p>Hello Admin</p>
								<p>You have received a new message from ".$postedData['cmt_name']." </p>
									<p>Cmment Note: ".$postedData['cmt_note']."</p>
									<p>E-mail id: ".$postedData['cmt_email']."</p>
							</body>
							</html>
							";
						
						//echo $body;exit;	
						$metaDataModel = new Model_Metadata();
						$ADMIN_MAIL_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_EMAIL");
						$ADMIN_MAIL = $ADMIN_MAIL_DATA['mtd_value'];
						
						$ADMIN_FROM_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_NAME");
						$ADMIN_FROM = $ADMIN_FROM_DATA['mtd_value'];
						
						$mail = new Zend_Mail();
						$mail->setBodyText($body);
						$mail->setBodyHtml($body, "iso-8859-1");
						$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
						$mail->setReplyTo($postedData['cmt_email'], $postedData['cmt_name']);
						$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
						$mailSubject = "Comment";
						$mail->addHeader('MIME-Version', '1.0');
						$mail->addHeader('Content-Transfer-Encoding', '8bit');
						$mail->addHeader('X-Mailer:', 'PHP/' . phpversion());
						$mail->setSubject($mailSubject);

                        //blog save
						$commentData = array();
                        $commentData['cmt_id'] = $cmt_id;
						$commentData['cmt_blog_id'] = $blog_id;
                        $commentData['cmt_name'] = $postedData['cmt_name'];				
						$commentData['cmt_email'] = $postedData['cmt_email'];
                        $commentData['cmt_note'] = $postedData['cmt_note'];
						if (array_key_exists('cmt_send' , $_POST)){
							$commentData['cmt_send'] = $postedData['cmt_send'];
						}
						$commentData['cmt_status'] = 1;
						$commentData['cmt_createddate'] = date('Y-m-d H:i:s');
						$commentData['cmt_updateddate'] = date('Y-m-d H:i:s');
						
						/*echo '<pre>';
						print_r($commentData);
						exit;*/
						
                        $cmt_id = $commentModel->save($commentData, 'cmt_id');
						$blogdetailForm->reset();
						
                       try {
							if($cmt_send = "selected"){
								$mail->send();
							}
							$successmessage ='Thanks for Send Message.';
							$this->addSuccessMessage($successmessage);
							//echo $successmessage;exit;
							$this->_helper->redirector('blogdetail', 'blog', 'default');
							
						} catch (Zend_Exception $e) {
							$errorMessage[] = "Due to technical problem, email has not been sent. Please try later or contact webmaster.";
						}
                    }
                }
            }	
			
		$blogData = $blogModel->fetchBlogWITHComment("blog_id = ".$blog_id);
        $recentData = $blogModel->fetchBlogWITHComment("", "6", "", "blog_id", "desc");
        //$blogImageData = $blogImageModel->fetchEntryById($blog_id);
        
        $BlogCommentData = $commentModel->fetchComment("cmt_blog_id = ".$blog_id);
               
        $next_blogid = $blogModel->NEXTblogid($blog_id);
        $prev_blogid = $blogModel->PREVIOUSblogid($blog_id);
       
        //$tagData = $tagModel->fetchTag('tag_status = 1');
        $categoryData = $categoryModel->fetchCategory('cat_status = 1');
        
        //$blogtagData = $blogtagModel->fetchtagreference($blog_id);
		
			$this->view->cmt_id = $cmt_id;
			$this->view->errorMessageArray = $errorMessageArray;
			$this->view->succesMessage = $successmessage;
			$this->view->form = $blogdetailForm;
       
			$this->view->BlogCommentData = $BlogCommentData;
			//$this->view->blogTagData = $blogtagData;
			//$this->view->tagData = $tagData;
			$this->view->categoryData = $categoryData;
			$this->view->next_blogid = $next_blogid;
			$this->view->prev_blogid = $prev_blogid;
			$this->view->recentData = $recentData;
			$this->view->errorMessage = $errorMessage;
			$this->view->successmessage = $successmessage;
			//$this->view->checkArray = $blogModel->getCheckArray();
			$this->view->searchtype = $searchtype;
			$this->view->blogData = $blogData;
			//$this->view->blogImageData = $blogImageData;
	}	
}

?>
